package com.zw.pdm.module.production.controller;

import com.zw.pdm.annotation.RepeatCommit;
import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.basic.DictCommon;
import com.zw.pdm.entity.production.MakeChange;
import com.zw.pdm.entity.production.MakePlan;
import com.zw.pdm.entity.production.MakeSheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.production.model.MakeChangeModel;
import com.zw.pdm.module.production.model.MakePlanModel;
import com.zw.pdm.module.production.model.MakeResultModel;
import com.zw.pdm.module.production.repository.MakeSheetRepository;
import com.zw.pdm.module.production.service.ProductionListService;
import com.zw.pdm.module.production.service.ProductionSheetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.zw.pdm.constant.Constant.*;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName ProductionListController.java
 * @Description TODO 制作件清单提交展示
 * @createTime 2020年10月06日 16:43:00
 */
@Controller
@RequestMapping("/production/list")
@SuppressWarnings("all")
public class ProductionListController extends BaseController {

    @Autowired
    private ProductionSheetService productionSheetService;
    @Autowired
    private ProductionListService productionListService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private MakeSheetRepository makeSheetRepository;

    /********************************************************需求变更以后的代码*************************************************************/

    /** myf在创建制作件清单页点击确认
     * 或者在左边菜单栏点击制作件清单提交时默认调用
     * 此方法查询未提交的制作件列表
     * */
    @GetMapping
    @RequiresPermissions({"production:list:view"})
    public String sheetPage(Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_MALTYPE));
        return "production/list/modifyMakeList";
    }


    /** 点击变更按钮， 展示制作件明细页面并且可以增删改 */
    @GetMapping("/modifySelect/{id}")
    @RequiresPermissions({"production:list:cha"})
    public String modifySelect(@PathVariable("id") Integer sheetId, Model model) {
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        int i = makeSheetRepository.countBySheetIdAndStatusNotAndIsDel(sheetId,5,0);
        if(i>0){
            model.addAttribute("isChange",-1);
        }
        model.addAttribute("makeSheet", makeSheet);
        //判断是原始单还是变更单 表单类型 1=原始，2=变更
        if(makeSheet.getSheetType()==1){
            List<MakePlanModel> list = productionListService.modifyFindByMakeSheetId(sheetId);
            model.addAttribute("makePlanORmakeChangeList", list);
        }else if (makeSheet.getSheetType()==2){
            List<MakeChangeModel> list = productionListService.findByMobifySheetId(sheetId);
            model.addAttribute("makePlanORmakeChangeList", list);
        }
        model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("makeTypes", dictCommonService.findDictByGroupName(GROUP_MAKETYPE));
        return "production/list/modifyMakeSelectPage";
    }


    /** myf制作件列表点击查看按钮时查看此项目下的制作件清单明细  */
    @GetMapping("/{id}")
    @RequiresPermissions({"production:list:view"})
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        //通过Id查询制作件主表
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("process", productionSheetService.findVpBySheetId(sheetId));
        model.addAttribute("MakeSheet", makeSheet);
        //判断是原始单还是变更单
        if (makeSheet.getSheetType() == 1) {
            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
            return "production/list/modifyMakePrint";
        } else {
            //查询变更原因
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", productionSheetService.findMakeResultChangeBySheetId(sheetId));
            return "production/list/modifyMakeChangePrint";
        }
    }

    /** 变更功能， 保存制作件计划变更单 */
    @ResponseBody
    @PostMapping("/make/change/modifySave")
    @RequiresPermissions({"production:list:cha"})
    public JsonResult modifySaveOutChange(MakeSheet sheet, Integer[] planId,
                                    String[] afterDrawNo, Integer[] afterDrawNum, String[] afterDrawName, String[] afterMakeType,
                                          String[] afterRemark, String[] afterUrl,Integer[] orderNum,Integer[] technologyChange,Integer[] pid) {
        int i = productionListService.modifySaveChange(sheet,planId, afterDrawNo, afterDrawNum, afterDrawName, afterMakeType,
                afterRemark, afterUrl, orderNum,technologyChange,pid);
        return JsonResult.success("操作成功！",i);
    }


    /** myf修改功能， 制作件清单修改页面 */
    @GetMapping("/modifyEdit/{id}")
    @RequiresPermissions({"production:list:edit"})
    public String modifyEditSheet(@PathVariable("id") Integer sheetId, Model model) {
        //通过Id查询制作件主表
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("makeSheet", makeSheet);
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("makeTypes", dictCommonService.findDictByGroupName(GROUP_MAKETYPE));
        model.addAttribute("makePrats",productionSheetService.findMakePartByProjectCode(makeSheet.getProCode(),makeSheet.getSpecialId()));
        if (makeSheet.getSheetType() == 1) {
            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
            return "production/list/modifyMakeEdit";

        } else {
            model.addAttribute("MakePlans", productionSheetService.findMakeResultChangeBySheetId(sheetId));
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            return "production/list/modifyMakeChangeEdit";
        }

    }


    /** myf修改功能， 制作件清单修改页面 */
    @GetMapping("/modifyChangeEdit/{id}")
    @RequiresPermissions({"production:list:edit"})
    public String modifyChangeEditSheet(@PathVariable("id") Integer sheetId, Model model) {
        //通过Id查询制作件主表
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("makeSheet", makeSheet);
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("makeTypes", dictCommonService.findDictByGroupName(GROUP_MAKETYPE));
        model.addAttribute("makePrats",productionSheetService.findMakePartByProjectCode(makeSheet.getProCode(),makeSheet.getSpecialId()));
        if (makeSheet.getSheetType() == 1) {
            model.addAttribute("makePlamake/change/modifyEditns", productionSheetService.findPlanBySheetId(sheetId));
            return "production/list/modifyMakeEdit";

        } else {
            model.addAttribute("MakePlans", productionSheetService.findMakeResultChangeBySheetId(sheetId));
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            return "production/list/modifyMakeChangeRedonlyEdit";
        }

    }




    /** 变更修改功能， 修改制作件变更计划单 */
    @ResponseBody
    @PostMapping("/make/change/modifyEdit")
    @RequiresPermissions({"production:list:edit"})
    public JsonResult modifyChangeEdit(MakeSheet sheet,
                                    String[] changeReason, Integer[] planId,
                                    String[] afterDrawNo, Integer[] afterDrawNum, String[] afterDrawName,
                                    String[] afterMakeType, String[] afterRemark) {
        return jsonResult(productionListService.editOutChange(sheet, changeReason,planId, afterDrawNo, afterDrawNum, afterDrawName, afterMakeType, afterRemark));
    }

    /** 提交功能  提交制作件清单 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/modifyCommit")
    @RequiresPermissions({"production:list:commit"})
    public JsonResult modifyCommitSheet(Integer id) {

        /** 点击提交的时候先验证项目下是否有相同的图号*/
//        List<String> repeatDrawNo = productionListService.getRepeatDrawNo(id);

        /** 点击提交的时候先验证项目部分下是否有相同的图号*/
        List<String> repeatDrawNo = productionListService.getRepeatPartDrawNo(id);

        if(!CollectionUtils.isEmpty(repeatDrawNo)){
            StringBuilder sb = new StringBuilder();
            String resultString = "";

            for(int i=0;i<repeatDrawNo.size();i++){
                if(i<repeatDrawNo.size()-1){
                    sb.append(repeatDrawNo.get(i));
                    sb.append("，");
                }else{
                    sb.append(repeatDrawNo.get(i));
                }
            }
            resultString = sb.toString();
            return JsonResult.success("本次提交有以下图号在本部分下重复，请进行修改或删除！否则无法提交！"+resultString,-10);
        }

        int i = productionListService.modifyCommitMakeSheetById(id);
        if(i==-7){
            return JsonResult.success("此条提交记录里面的明细没有填写变更原因！请去修改页面填写！", -7);
        }

        return jsonResult(i);
    }










    /********************************************************提交列表页面的功能*************************************************************/

    /** myf在创建制作件清单页点击确认
     * 或者在左边菜单栏点击制作件清单提交时默认调用
     * 此方法查询未提交的制作件列表
     * */
//    @GetMapping
//    @RequiresPermissions({"production:list:view"})
//    public String sheetPage(Model model) {
//        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_MALTYPE));
//        return "production/list/makeList";
//    }

    @PostMapping("/list/reason")
    @ResponseBody
    @RequiresPermissions({"production:list:view"})
    public List<DictCommon> sheetPage() {
        List<DictCommon> reasonList = dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON);
        return reasonList;
    }

    /** myf在创建制作件清单页点击确认
     * 或者在左边菜单栏点击制作件清单提交时默认调用
     * 此方法查询未提交的制作件列表
     * */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"production:list:view"})
    public JsonResult sheetList(MakeSheet sheet) {
        /** 查询管理部门数量*/
        Integer i = productionListService.countOrgByUserId();
        startPage();
        List<MakeSheet> list = productionListService.findWithReportList(sheet, i);
        return jsonResult(list);
    }

    /** myf制作件列表点击查看按钮时查看此项目下的制作件清单明细  */
//    @GetMapping("/{id}")
//    @RequiresPermissions({"production:list:view"})
//    public String info(@PathVariable("id") Integer sheetId, Model model) {
//        //通过Id查询制作件主表
//        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
//        model.addAttribute("process", productionSheetService.findVpBySheetId(sheetId));
//        model.addAttribute("MakeSheet", makeSheet);
//        //判断是原始单还是变更单
//        if (makeSheet.getSheetType() == 1) {
//            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
//            return "production/list/makePrint";
//        } else {
//            //查询变更原因
//            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
//            model.addAttribute("changes", productionSheetService.findMakeResultChangeBySheetId(sheetId));
//            return "production/list/makeChangePrint";
//        }
//    }

    /** myf修改功能， 制作件清单修改页面 */
    @GetMapping("/edit/{id}")
    @RequiresPermissions({"production:list:edit"})
    public String editSheet(@PathVariable("id") Integer sheetId, Model model) {
        //通过Id查询制作件主表
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("makeSheet", makeSheet);
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("makeTypes", dictCommonService.findDictByGroupName(GROUP_MAKETYPE));
        model.addAttribute("makePrats",productionSheetService.findMakePartByProjectCode(makeSheet.getProCode(),makeSheet.getSpecialId()));
        if (makeSheet.getSheetType() == 1) {
            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
            return "production/list/makeEdit";

        } else {
            model.addAttribute("MakePlans", productionSheetService.findMakeResultChangeBySheetId(sheetId));
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            return "production/list/makeChangeEdit";
        }

    }

    /** myf删除功能， 删除制作件单据， 包含计划单中的所有计划 */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"production:list:del"})
    public JsonResult delSheet(Integer id) {
        return jsonResult(productionListService.delSheetById(id));
    }

    /** 提交功能  提交制作件清单 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/commit")
    @RequiresPermissions({"production:list:commit"})
    public JsonResult commitSheet(Integer id) {
        return jsonResult(productionListService.commitMakeSheetById(id));
    }


    /********************************************************修改页面的功能*************************************************************/


    /** 修改功能， 删除计划单中的单个计划 */
    @ResponseBody
    @PostMapping("/makePlan/del")
    @RequiresPermissions({"production:list:edit"})
    public JsonResult delPlan(Integer id) {
        return jsonResult(productionListService.delPlanById(id));
    }

    /** 修改功能， 上传附件 */
    @ResponseBody
    @PostMapping("/makePlan/upload")
    @RequiresPermissions({"production:list:edit"})
    public JsonResult planUpload(MakePlan plan, @RequestParam("file") MultipartFile file) {
        return isNotNull(productionListService.uploadWithPlan(plan, file));
    }


    /** 修改功能， 修改制作件计划 */
    @ResponseBody
    @PostMapping("/make/edit")
    @RequiresPermissions({"production:list:edit"})
    public JsonResult makeEdit(MakeSheet makeSheet, Integer[] makePlanId, String[] drawNo, Integer[] drawNum, String[] drawName, String[] makeType, String[] remark) {
        return jsonResult(productionListService.editMake(makeSheet, makePlanId, drawNo, drawNum, drawName, makeType, remark));
    }




    /** 变更功能， 选择制作件明细页面 */
    @GetMapping("/select/{id}")
    @RequiresPermissions({"production:list:cha"})
    public String select(@PathVariable("id") Integer sheetId, Model model) {
        model.addAttribute("makeSheet", productionSheetService.getSheetById(sheetId));
        return "production/list/makeSelectPage";
    }



    /** 变更功能， 变更页面 */
    @GetMapping("/changePage")
    @RequiresPermissions({"production:list:cha"})
    public String changePage(Integer sheetId, String ids, Model model) {
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("makeSheet", makeSheet);
        if (makeSheet.getSheetType() == 1) {
            model.addAttribute("makePlans1", productionListService.findMakePlanByIds(ids));
        }
        if (makeSheet.getSheetType() == 2) {
            model.addAttribute("makePlans1", productionListService.findMakeChangeByIds(ids));
        }
        model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "production/list/makeChange";
    }


    @GetMapping("/changeReasons")
    @ResponseBody
    @RequiresPermissions({"production:list:cha"})
    public List<DictCommon> changeReasons(){
        return dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON);
    }



    /** 变更功能， 详细计划列表 */
    @ResponseBody
    @PostMapping("/planList/{id}")
    @RequiresPermissions({"production:list:cha"})
    public JsonResult planList(@PathVariable("id") Integer sheetId) {
        startPage();
        List<MakePlan> list = productionListService.findPlanListBySheetId(sheetId);
        return jsonResult(list);
    }

    /** 变更功能， 详细列表 */
    @ResponseBody
    @PostMapping("/changeList/{id}")
    @RequiresPermissions({"production:list:cha"})
    public JsonResult changeList(@PathVariable("id") Integer sheetId) {
        startPage();
    List<MakeChange> list = productionListService.findChangeListBySheetId(sheetId);
        return jsonResult(list);
    }

    /** 变更功能， 保存制作件计划变更单 */
    @ResponseBody
    @PostMapping("/make/change/save")
    @RequiresPermissions({"production:list:cha"})
    public JsonResult saveOutChange(MakeSheet sheet,
                                    String[] changeReason, Integer[] planId,
                                    String[] afterDrawNo, Integer[] afterDrawNum, String[] afterDrawName,
                                    String[] afterMakeType, String[] afterRemark, String[] afterUrl,Integer[] orderNum,Integer[] technologyChange) {
        int i = productionListService.saveOutChange(sheet, changeReason, planId, afterDrawNo, afterDrawNum, afterDrawName, afterMakeType, afterRemark, afterUrl, orderNum,technologyChange);
        return jsonResult(i);
    }

    /** 变更修改功能， 修改制作件变更计划单 */
    @ResponseBody
    @PostMapping("/make/change/edit")
    @RequiresPermissions({"production:list:edit"})
    public JsonResult outChangeEdit(MakeSheet sheet,
                              String[] changeReason, Integer[] planId,
                              String[] afterDrawNo, Integer[] afterDrawNum, String[] afterDrawName,
                              String[] afterMakeType, String[] afterRemark) {
        return jsonResult(productionListService.editOutChange(sheet, changeReason,planId, afterDrawNo, afterDrawNum, afterDrawName, afterMakeType, afterRemark));
    }

    /** 修改功能， 删除计划单中的单个计划 */
    @ResponseBody
    @PostMapping("/makeChange/del")
    @RequiresPermissions({"production:list:edit"})
    public JsonResult delChange(Integer id) {
        return jsonResult(productionListService.delMakeChangeById(id));
    }



/***********************************************************************************************************************/



    /** 变更功能， 变更页面增加删除 */
    @GetMapping("/changePageResult")
    @RequiresPermissions({"production:list:cha"})
    public String changePageResult(Integer sheetId, String ids, Model model) {
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("makeSheet", makeSheet);
        if (makeSheet.getSheetType() == 1) {
            model.addAttribute("makePlans", productionListService.findMakeResultPlanByIds(ids));
        }
        if (makeSheet.getSheetType() == 2) {
            model.addAttribute("makePlans", productionListService.findMakeResultChangeByIds(ids));
        }
        model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "production/list/makeChange";
    }


    /** 查询result表里面的数据变更功能， 详细计划列表 */
    @ResponseBody
    @PostMapping("/makeResultList/{id}")
    @RequiresPermissions({"production:list:cha"})
    public JsonResult makeResultList(@PathVariable("id") Integer sheetId) {
        startPage();
        List<MakeResultModel> list = productionSheetService.getMakeResultSheetId(sheetId);
        return jsonResult(list);
    }



    /** 变更功能， 选择制作件明细页面 */
    @GetMapping("/select1/{id}")
    @RequiresPermissions({"production:list:cha"})
    public String select1(@PathVariable("id") Integer sheetId, Model model) {
        model.addAttribute("makeResult", productionSheetService.getMakeResultSheetId(sheetId));
        return "production/list/makeSelectPage";
    }


}
